package com.hmall.api.config;

import com.hmall.api.clients.fallback.ItemClientFallbackFactory;
import com.hmall.api.clients.fallback.PayClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    // Feign拦截器 用于微服务之间的用户信息传递
    @Bean
    public RequestInterceptor UserInfoInterceptor() {
        return requestTemplate -> {
            Long userId = UserContext.getUser();
            requestTemplate.header("user-info",userId.toString() );
        };
    }
    // Feign降级处理
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory() {
        return new ItemClientFallbackFactory();
    }
    @Bean
    public PayClientFallback payClientFallback() {
        return new PayClientFallback();
    }
}
